package com.abe.libquick.main.video.player;

import android.text.TextUtils;

import com.abe.libquick.R;
import com.abe.libquick.R2;
import com.abe.libquick.base.BaseActivity;
import com.abe.libquick.utils.MediaDecoder;
import com.abe.libquick.utils.quick.EmptyUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZUtils;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class VideoPlayerActivity extends BaseActivity {
    @BindView(R2.id.item_video)
    JzvdStd itemVideo;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_video_player;
    }

    @Override
    protected void initContent() {
        if (EmptyUtils.isEmpty(bundle()) || !bundle().containsKey(VideoRecordHelper.VIDEO_PATH)) {
            finish();
            return;
        }
        String path = bundle().getString(VideoRecordHelper.VIDEO_PATH, "");
        if (TextUtils.isEmpty(path)) {
            finish();
            return;
        }
        boolean isNetVideo = path.contains("http");
        MediaDecoder.create()
                .setType(isNetVideo ? MediaDecoder.TYPE_VIDEO_NET : MediaDecoder.TYPE_VIDEO_LOCAL)
                .setDataSource(path)
                .setListener(bitmap -> runOnUiThread(() -> {
                    itemVideo.setUp(path, "");
                    itemVideo.posterImageView.setImageBitmap(bitmap);
                    itemVideo.startVideoAfterPreloading();
                })).start(MediaDecoder.VIDEO_START);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZUtils.clearSavedProgress(this, null);
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @OnClick(R2.id.item_close)
    public void close() {
        onBackPressed();
    }
}